/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using EvaluationException = System.Qizx.Api.EvaluationException;
using Node = System.Qizx.Api.Node;
using System.Qizx.XQuery;
namespace System.Qizx.XQuery.Op
{
	
	
	/// <summary> The transform expression of XQuery Update.</summary>
	public class TransformExpr:Expression
	{
		public LetClause[] copies;
		public Expression modify;
		public Expression result;
		
		public override Expression StaticCheck(ModuleContext context, int flags)
		{
			LocalVariable mark = context.latestLocalVariable();
			// for and let clauses, declare local variables
			for (int c = 0; c < copies.Length; c++)
			{
				((VarClause) copies[c]).owner = this;
				context.staticCheck(copies[c], 0);
			}
			
			// modify clause: updating
			modify = context.simpleStaticCheck(modify, 0);
			
			// returned expression:
			result = context.staticCheck(result, 0);
			type = result.getType();
			
			context.popLocalVariables(mark);
			return this;
		}
		
		public override XQValue eval(Focus focus, EvalContext context)
		{
			// create a local update list (zapped after execution)
			Updates pul = context.pushUpdates();
			
			// evaluate the copy clauses: declare roots in update list
			// and bind values with variables (no actual copy)
			for (int c = 0; c < copies.Length; c++)
			{
				Node value_Renamed = copies[c].expr.EvalAsNode(focus, context);
				LocalVariable var = copies[c].varDecl;
				context.storeLocalItem(var.address, (XQItem) value_Renamed);
				pul.addRoot(value_Renamed);
			}
			
			// evaluate the modify clause: values are irrelevant but must unroll
			XQValue modified = modify.eval(focus, context);
			while (modified.Next())
				;
			
			// apply updates and rebind 'copy' variables with modified values:
			for (int c = 0; c < copies.Length; c++)
			{
				Node value_Renamed = pul.applyTransformUpdates(c);
				LocalVariable var = copies[c].varDecl;
				context.storeLocalItem(var.address, (XQItem) value_Renamed);
			}
			
			// finally evaluate the return clause:        
			return result.eval(focus, context);
		}
		
		public override Expression Child(int rank)
		{
			int p = rank - copies.Length;
			if (p < 0)
				return copies[rank];
			return (p == 0)?modify:((p == 1)?result:null);
		}
		
		public override void  Dump(ExprDisplay d)
		{
			d.header(this);
			d.children("copies", copies);
			d.child("modify", modify);
			d.child("return", result);
		}
	}
}